package com.liming.purchaseschema.webapp.wicket.page;

import com.googlecode.wicket.jquery.ui.JQueryIcon;
import com.googlecode.wicket.jquery.ui.widget.menu.ContextMenu;
import com.googlecode.wicket.jquery.ui.widget.menu.ContextMenuBehavior;
import com.googlecode.wicket.jquery.ui.widget.menu.IMenuItem;
import com.googlecode.wicket.jquery.ui.widget.menu.MenuItem;
import com.liming.purchaseschema.core.model.SchemaDefine;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * @类描述：没有子的树节点
 * @创建人：wangkr
 * @创建时间：2014-04-11 下午1:52
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class TreeNoChildNode extends Panel {
    public TreeNoChildNode(String id,final SchemaDefine schemaDefine,final MyFormPanel formPanel,final TreePanel treePanel){
        super(id);
        final ContextMenu menu = new ContextMenu("menu", getMenuItemList(schemaDefine,treePanel,formPanel)) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onContextMenu(AjaxRequestTarget target, Component component)
            {
                //the menu-item list can be modified here
                //this.getItemList().add(new MenuItem("my new item"));
            }

            @Override
            public void onClick(AjaxRequestTarget target, IMenuItem item)
            {
                this.info("Clicked " + item.getTitle().getObject());

                target.add(this);
            }
        };
        add(menu);

        final Label label1 = new Label("nodeName",schemaDefine.getName());
        label1.add(new ContextMenuBehavior(menu));
        this.add(label1);

        AjaxLink ajaxLink = new AjaxLink("changeForm") {
            @Override
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                formPanel.StructureForm("123s",schemaDefine,treePanel,formPanel);
                ajaxRequestTarget.add(formPanel);
            }
        };
        ajaxLink.add(label1);
        add(ajaxLink);
    }

    private List<IMenuItem> getMenuItemList (final SchemaDefine schemaDefine,final TreePanel treePanel,final MyFormPanel formPanel){
        List<IMenuItem> list = new ArrayList<IMenuItem>();

        list.add(new MenuItem("增加节点"){
            @Override
            public void onClick(AjaxRequestTarget target)
            {
                formPanel.AddForm("123s",new SchemaDefine(),schemaDefine,treePanel,formPanel);
                target.add(formPanel);
            }
        });
        list.add(new MenuItem("删除节点"){
            @Override
            public void onClick(AjaxRequestTarget target)
            {
                treePanel.removeTreeNode(schemaDefine);
                treePanel.createTree(formPanel);
                target.add(treePanel);
            }
        });
        list.add(new MenuItem("编辑节点") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target)
            {
                formPanel.StructureForm("123s",schemaDefine,treePanel,formPanel);
                target.add(formPanel);
            }
        });
        return list;
    }
}


